<template>
    <div>
        <basic-layout title="异地就餐" @handleAdd="handleAdd">
            <div class="columnFlex">
                <!-- 查询条件 -->
                <van-row class="search-row">
                    <van-search v-model="formData.keyword" maxlength="20" show-action label=""
                        placeholder="食堂名称" @search="onRefresh">
                        <template #action>
                            <div @click="onRefresh">搜索</div>
                        </template>
                    </van-search>
                    <div class="filter-icon">
                        <van-icon name="filter-o" @click="$refs.queryPopupRef.show = true" />
                    </div>
                </van-row>
                <van-row class="listCon">
                    <van-pull-refresh class="pullRefresh" v-model="refreshLoading" @refresh="onRefresh">
                        <van-list v-model="loading" :finished="finished" :finished-text="listData.length ? '没有更多了' : ''"
                            @load="onLoad">
                            <van-swipe-cell v-for="(val, ind) in listData" :key="ind">
                                <div class="cellContent">
                                    <div class="top" @click="handleDetail(val)">
                                        <p class="tit">
                                            <b>
                                                {{ val.canteenName || '-' }}
                                            </b>
                                            <a class="status" :class="'status' + val.eatApplyStatus">{{
                                                val.eatApplyStatusName}}</a>
                                        </p>
                                    </div>
                                    <div class="bottom">
                                        <div class="left" @click="handleDetail(val)">
                                            <div>
                                                <p>
                                                    <span class="label">就餐开始时间：</span>
                                                    {{ val.eatStartDate }}
                                                </p>
                                                <p>
                                                    <span class="label">就餐结束时间：</span>
                                                    {{ val.eatEndDate }}
                                                </p>
                                                <p>
                                                    <span class="label">联系电话：</span>
                                                    <span>{{ val.applntPhone }}</span>
                                                </p>
                                            </div>
                                        </div>
                                        <div class="right" v-if="val.eatApplyStatus === '2'">
                                            <img src="images/convaccess/qrcode.svg"
                                                @click="$refs.codeViewRef.getData(val.strangeEatId);">
                                        </div>
                                        <div class="right" v-if="val.eatApplyStatus === '1'">
                                            <van-button type="info" color="#f25454" plain size="mini" @click="sub(val)">取消</van-button>
                                        </div>
                                    </div>
                                    <!-- <div class="flexBtn" v-if="val.eatApplyStatus === '1'">
                                        <p @click="sub(val)">取消</p>
                                        <p @click="del(val)">删除</p>
                                    </div> -->
                                </div>
                            </van-swipe-cell>
                        </van-list>
                        <van-empty v-if="!loading && !listData.length" class="custom-image"
                            :image="require('../../../../assets/images/empty.png')" description="暂无数据" />
                    </van-pull-refresh>
                </van-row>
            </div>
        </basic-layout>
        <!-- 筛选条件弹出框 -->
        <queryPopup ref="queryPopupRef" @handleSearch="onRefresh" @handleReset="handleReset">
            <van-form class="filterBox">
                <label>申请时间</label>
                <van-field>
                    <template #input>
                        <div class="textCon" @click="showStatusDatePicker = true">
                            <span v-if="!applyDateText" style="color: #aaa">请选择申请时间</span>
                            <span v-else>{{ applyDateText }}</span>
                        </div>
                    </template>
                </van-field>
                <label>申请状态</label>
                <van-field>
                    <template #input>
                        <div class="textCon" @click="showStatusPicker = true">
                            <span v-if="!statusText" style="color: #aaa">请选择申请状态</span>
                            <span v-else>{{ statusText }}</span>
                        </div>
                    </template>
                    <template #button>
                        <van-icon v-if="statusText" name="cross" @click="clearPicker()" />
                    </template>
                </van-field>
            </van-form>
        </queryPopup>
        <!-- 预定日期 -->
        <van-calendar v-model="showStatusDatePicker" :allow-same-day="true"  :min-date="minDate" type="range" @confirm="onTimeConfirm" />
        <!-- 预定状态选择 -->
        <van-action-sheet v-model="showStatusPicker" cancel-text="取消" :actions="applyStatusList"
            @select="onStatusConfirm" />
        <!-- 新增/详情/编辑 弹出框 -->
        <fullscreenModal :modal="fullscreenModal" :isAdd="isAdd" label="异地就餐" :isShowEdit="isShowEdit"
            :editType="editType" @handleClose="fullscreenModal = false" @handleEdit="handleEdit"
             @handleCancel="editType = 'detail'" @handleSave="$refs.editFormRef.handleSave()">
            <!-- <detail v-show="editType == 'detail'" ref="detailRef"></detail> -->
            <detail-info v-show="editType == 'detail'" ref="detailRef" />
            <edit-form v-show="editType == 'edit'" @close="fullscreenModal = false" @refresh="onRefresh"
                ref="editFormRef" />
        </fullscreenModal>
        <full-loading v-if="fullLoading"></full-loading>
        <!-- 明细 -->
        <codeView ref="codeViewRef" title="就餐码" />
    </div>
</template>

<script>
    import { queryDic } from '@/api/common';
    import { getPage, delByStrangeId, getCanteenList, getInfoById, updCancel } from '@/api/srvAssurance/smarcatering/strangeEat';
    import editForm from "./editForm.vue";
    import codeView from "./codeView.vue";
    import detailInfo from "./detailInfo.vue";
    export default {
        components: {
            editForm,
            codeView,
            detailInfo,
        },
        data() {
            return {
                isShowEdit: false,
                showStatusDatePicker: false,
                loading: false,
                fullLoading: false,
                refreshLoading: false,
                fullscreenModal: false,
                showStatusPicker: false,
                showCancelModal: false,
                finished: false,
                pageNum: 0,
                pageSize: 20,
                formData: {
                    state: '',
                    keyword: '',
                    startTime: null,
                    endTime: null,
                },
                canteenText: "",
                statusText: "",
                applyDateText: '',
                listData: [], // 列表数据
                applyStatusList: [], // 申请状态
                isAdd: false,
                editType: "",
                cancelFormData: {
                    bookeId: '', // 取消标识
                    cancelReason: '', // 取消原因
                },
                editParam: {},
            };
        },
        computed: {
            minDate() {
                let nowDate = new Date();
                let array = [];
                let year = nowDate.getFullYear();
                let mon = nowDate.getMonth() + 1;
                for (let i = 0; i < 6; i++) {
                    mon = mon - 1;
                    if (mon <= 0) {
                        year = year - 1;
                        mon = mon + 12;
                    }
                    if (mon < 10) {
                        mon = "0" + mon;
                    }
                    array[i] = year + "-" + mon;
                }

                let day = nowDate.getDate();
                let d = array.pop() + '-' + day;
                return new Date(d);
            }
        },
        // create(){
        //     this.getListData();
        // },
        mounted() {
            this.queryDicData();
        },
        methods: {
            handleEdit() {
                getInfoById(this.editParam.strangeEatId)
                    .then((res) => {
                        if (res.code === 1000 && res.data) {
                            this.editType = "edit";
                            this.$refs.editFormRef.resetForm();
                            // this.$refs.editFormRef.getData(this.currentParam);
                            this.$refs.editFormRef.getData({ data: res.data });
                            // this.fullscreenModal = true;

                        }
                    })
                    .catch(() => {

                    });

            },
            handleDetail(item) {
                this.fullscreenModal = true;
                this.editType = "detail";
                this.isAdd = false;
                if (item.eatApplyStatus === '1') {
                    this.isShowEdit = true;
                } else {
                    this.isShowEdit = false;
                }

                this.editParam = item;
                this.$refs.detailRef.getData(item);

            },
            sub(item) {
                this.$Dialog
                    .confirm({
                        title: "是否确定取消该数据？",
                    })
                    .then(async () => {
                        var res = await updCancel(item.strangeEatId);
                        if (res.code === 1000) {
                            this.$Notify({ type: "success", message: "取消成功！" });
                            this.onRefresh();
                        }
                    })
                    .catch(() => { });
            },
            del(item) {
                this.$Dialog
                    .confirm({
                        title: "是否确定删除该数据？",
                    })
                    .then(async () => {
                        var res = await delByStrangeId(item.strangeEatId);
                        if (res.code === 1000) {
                            this.$Notify({ type: "success", message: "删除成功！" });
                            this.onRefresh();
                        }
                    })
                    .catch(() => { });
            },
            // 预定日期选择
            onTimeConfirm(date) {
                const [start, end] = date;
                this.showStatusDatePicker = false;
                this.formData.startTime = new Date(start).format('yyyy-MM-dd');
                this.formData.endTime = new Date(end).format('yyyy-MM-dd');
                this.applyDateText = `${this.formData.startTime} - ${this.formData.endTime}`;
                this.showStatusDatePicker = false;
                this.onRefresh();
            },
            // 获取表格数据
            getListData() {
                this.loading = true;
                let params = {
                    ...this.formData,
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                }
                getPage(params).then((res) => {
                    this.loading = false;
                    if (res.code === 1000 && res.data) {
                        this.listData = [...this.listData, ...res.data.records];
                        if (this.listData.length >= res.data.total) {
                            this.finished = true;
                        }
                    } else {
                        this.finished = true;
                    }
                }).catch(() => {
                    this.loading = false;
                    this.finished = true;
                });
            },
            onRefresh() {
                this.refreshLoading = false;
                this.finished = false;
                this.listData = [];
                this.pageNum = 1;
                this.getListData();
            },
            onLoad() {
                this.pageNum++;
                this.getListData();
            },
            // 重置
            handleReset() {
                this.formData = {
                    keyword: '',
                    state: ''
                };
                this.pageNum = 1;
                this.pageSize = 20;
                this.statusText = "";
                this.applyDateText = "";
                this.onRefresh();
            },
            // 预定状态选择
            onStatusConfirm(item) {
                this.formData.state = item.value;
                this.statusText = item.name;
                this.showStatusPicker = false;
                this.onRefresh();
            },
            // 清空查询条件
            clearPicker() {
                this.formData.state = "";
                this.formData.keyword = "";
                this.statusText = "";
                this.applyDateText = "";
                this.formData.startTime = null;
                this.formData.endTime = null;
                this.onRefresh();
            },
            // 新建数据
            handleAdd() {
                this.fullscreenModal = true;
                this.editType = "edit";
                this.isAdd = true;
                this.$refs.editFormRef.resetForm();
            },

            // 下拉框数据
            queryDicData() {
                // 食堂列表
                getCanteenList().then(res => {
                    if (res.code === 1000 && res.data) {
                        res.data.forEach(item => {
                            item.name = item.canteenName
                        });
                        this.$nextTick(() => {
                            this.$refs.editFormRef.canteenList = res.data;
                        })
                    }
                })
                // 申请状态
                queryDic('SA_EAT_APPLY_STATUS_CODE').then(res => {
                    if (res.code === 1000 && res.data) {
                        this.applyStatusList = res.data;
                    }
                })
            }
        },
    };
</script>

<style scoped lang="less">
    .listCon {
        .cellContent {
            padding: 0;
            position: relative;

            .top {
                padding: 5px 10px;
                box-sizing: border-box;
                border-bottom: 1px solid #eee;
                position: relative;

                .time {
                    position: absolute;
                    right: 5px;
                    top: 8px;
                    color: #aaa;
                    font-size: 13px;
                }

                .tit {
                    font-size: 15px;
                    height: 30px;

                    b span {
                        font-size: 14px;
                    }

                    .status {
                        background: rgb(70, 172, 240);
                        font-size: 9px;
                        color: #fff;
                        margin-left: 8px;
                        padding: 2px 5px;
                        border-radius: 3px;
                    }

                    .status2 {
                        background: #1aa78f;
                    }

                    .status3 {
                        background: #f25454;
                    }

                    .status4 {
                        background: #b4afaf;
                    }
                }

                .reason {
                    font-size: 13px;
                    color: #aaa;
                    line-height: 20px;
                }

                .type {
                    background: #f2a84e;
                    font-size: 9px;
                    color: #fff;
                    margin-left: 8px;
                    padding: 2px 3px;
                    border-radius: 3px;
                }
            }

            .bottom {
                padding: 5px 10px;
                box-sizing: border-box;
                display: flex;
                overflow: hidden;
                align-items: center;
                border-bottom: 1px solid #eee;

                .left {
                    flex: 1;
                    overflow: hidden;
                }

                .right {
                    margin: 0 5px 0 10px;

                    img {
                        width: 35px;
                        height: 35px;
                    }

                    p {
                        color: #aaa;
                        font-size: 13px;
                    }
                    .van-button {
                        padding: 0 10px;
                        height: 30px;
                        line-height: 30px;
                        border-radius: 17px;
                        font-size: 13px;
                    }
                }
            }

            .flexBtn {
                display: flex;
                justify-content: space-between;

                p {
                    flex: 1;
                    text-align: center;
                    height: 40px;
                    line-height: 40px;
                    color: #4eb3e9;
                    font-size: 15px;
                }

                p:nth-of-type(1) {
                    box-sizing: border-box;
                    border-right: 1px solid #eee;
                    color: #f25454;
                }
            }
        }

        /* .flexBtn {
            display: flex;
            justify-content: space-between;

            p {
                flex: 1;
                text-align: center;
                height: 40px;
                line-height: 40px;
                color: #4eb3e9;
                font-size: 15px;
            }

            p:nth-of-type(1) {
                box-sizing: border-box;
                border-right: 1px solid #eee;
                color: #f25454;
            }
        } */
    }
</style>